﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml.Linq;

namespace ReporterApplication
{
    public partial class SelectorTestForm : Form
    {
        public SelectorTestForm()
        {
            InitializeComponent();
        }

        public string CifFolder { get; set; }

        XmlCIF cif;

        private void SelectorTestForm_Load(object sender, EventArgs e)
        {
            cif = XmlCIF.Load(Path.Combine(CifFolder, "cif.xml"));

            cif.PrepareMetadataModel();
        }


        //Requirements$childs+subject$Requirement models$members$Requirement
        //Business Classes+start$Characteristic#end$Attributes
        //Packages+subject$members$Business classes
        private void eval_button_Click(object sender, EventArgs e)
        {
            evaluate();
        }

        private void evaluate()
        {
            props_listBox.Items.Clear();

            resulttextBox.Text = "not evalable";

            try
            {

                var cifElems = cif.GetElementsByPath(path_textBox.Text, cif.Notebook);

                StringBuilder sb = new StringBuilder();

                var attribsList = new List<XAttribute>();
                var elementsList = new List<XElement>();

                foreach (var el in cifElems)
                {

                    sb.AppendLine(cif.getTypeName(el) + " : " + cif.getFullName(el));

                    foreach (var att in el.Attributes())
                    {
                        if (attribsList.Where(x => x.Name.LocalName == att.Name.LocalName).Count() == 0)
                        {
                            attribsList.Add(att);
                        }
                    }

                    foreach (var att in el.Elements())
                    {
                        if (elementsList.Where(x => x.Name.LocalName == att.Name.LocalName).Count() == 0)
                        {
                            elementsList.Add(att);
                        }
                    }
                }

                foreach (var attrib in attribsList)
                {
                    props_listBox.Items.Add("#" + attrib.Name.LocalName);
                }

                foreach (var attrib in elementsList)
                {
                    props_listBox.Items.Add("#" + attrib.Name.LocalName);
                }




                resulttextBox.Text = sb.ToString();
            }
            catch (Exception e)
            {

            }
        }

        private void path_textBox_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void path_textBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == (int)Keys.Enter)
            {
                evaluate();
            }
        }
    }
}
